<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改学生</title>
    <script src="js/jquery-3.6.3.min.js.txt"></script>
    <script>
        $(function () {
            var g = $("[name=gradeid]");
            var gradeajax = $.get("findGradesALL.do", function (list) {
                $.each(list, function (i, o) {
                    g.append(`<option value="${o.id}">${o.gname}</option>`);
                });
            });
 
            var stuno = window.location.search;
            stuno = stuno.substring(1);
            $.when(gradeajax).done(function () {
                $.get("findStu.do", {stuno: stuno}, function (r) {
                    //alert(JSON.stringify(stu));
                    if (r.success ==false) {
                        alert("没有此人");
                        return false;
                    }
                    let stu=r.data;
                    //$("input[name=stuno]").val(stu.stuno);
                    for (let k in stu) {
                        console.log(k);
                        if (k == "sex") {
                            $(`[name=${k}][value='${stu[k]}']`).prop("checked", true);
                            continue;
                        }
                        if(k=="idcardpic"){
                            $("#preview").attr("src",stu.idcardpic);
                            continue;
                        }
                        $(`[name=${k}]`).val(stu[k]);
                    }
                    //$("#preview").attr("src",stu.idcardpic);
                });
            });
            $("#updateForm").submit(function () {
                let fd = new FormData(this);
                ////
                $.ajax({
                    type: 'post',
                    url: "updStudent.do",
                    data: fd,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success:function (data) {
                        if(data.success){
                            alert("修改成功");
                            window.location.replace("students.html");
                        }else{
                            alert(data.msg);
                        }
                    },
                    error:function (e) {
                        alert("出现错误："+e);
                    }
                });
                return false;
            });

        });
    </script>
</head>
<body>
<form action="updateStudent.do" id="updateForm" method="post" enctype="multipart/form-data">
    <div>
        <input type="text" name="stuno" readonly hidden>
    </div>

    <div>
        <label>学生姓名:</label>
        <input type="text" name="sname">
    </div>
    <div>
        <label>性别:</label>

        <input type="radio" name="sex" value="男">男
        <input type="radio" name="sex" value="女">女
        <input type="radio" name="sex" value="妖">妖

    </div>
    <div>
        <label>电话号码:</label>

        <input type="tel" name="telephone">
    </div>
    <div>
        <label>QQ:</label>
        <input type="number" name="QQ">
    </div>
    <div>
        <label>weChat:</label>
        <input type="text" name="wechat">
    </div>
    <div>
        <select name="gradeid" >

        </select>
    </div>
    <div>
        <label>入学日期:</label>
        <input type="date" name="enterdate">
    </div>
    <div>
        <label>来自城市:</label>
        <input type="text" name="fromcity">
    </div>
    <div>
        <label>出生日期:</label>
        <input type="date" name="birthday">
    </div>
    <div>
        <label>家庭住址:</label>
        <input type="text" name="address">
    </div>
    <div>
        <label>密码:</label>
        <input type="password" name="password">
    </div>
    <div>
        <label>身份证:</label>
        <input type="file" name="idCardPic" onchange="previewImage(this)">
    </div>
    <div>
        <img id="preview" src="#" alt="预览图片" width="500px" height="600px"/>
    </div>

    <div>
        <input type="submit" value="修改">
    </div>
</form>
</body>
</html>